﻿using AutoMapper;
using MediatR;
using RedDragon.Common.Domain.AuthorizeRBAC;
using RedDragon.Common.Infrastructure.Interface;
using RedDragon.RBAC.API.Application.Common.Permission;
using RedDragon.RBAC.ErrorCodeEnum;
using T.Common.Domain.Common;

namespace RedDragon.RBAC.API.Application.Common.Role
{
    public class QueryRolesHandler : IRequestHandler<QueryRolesCommand, Result>
    {
        /// <summary>
        /// 角色表仓储
        /// </summary>
        public readonly IBaseRepository<Roles> rolesrepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<QueryRolesHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="rolesrepository"></param>
        /// <param name="logger"></param>
        
        public QueryRolesHandler(IBaseRepository<Roles> rolesrepository, ILogger<QueryRolesHandler> logger)
        {
            this.rolesrepository = rolesrepository;
            this.logger = logger;
        }

       /// <summary>
       ///  无查询条件获取角色列表
       /// </summary>
       /// <param name="request"></param>
       /// <param name="cancellationToken"></param>
       /// <returns></returns>
       /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(QueryRolesCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                var list = await(await rolesrepository.GetAll()).ToListAsync();
                if (list != null)
                {
                    result.Data = list;
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "用户列表获取成功";
                }
            }
            catch (Exception ex)
            {
                logger.LogError("用户列表获取异常");
                result.Msg = "用户列表获取异常";
            }
            return result;
        }
    }
}
